package com.leetcode;

import sun.java2d.pipe.SpanIterator;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class 无重复字符的最长子串 {
    public static void main(String[] args) {
        String s = "dvdf";
        System.out.println(lengthOfLongestSubstring(s));

    }

    public static int lengthOfLongestSubstring(String s) {
        Set<Character> hashSet = new HashSet<>();
        int left = 0, right = 0, size = 0, max_size = 0;
        char[] charArray = s.toCharArray();
        while (right < s.length()) {
            if (!hashSet.contains(charArray[right])) {
                hashSet.add(charArray[right]);
                right++;
                size++;
                max_size = Math.max(max_size, size);
            } else {
                while (hashSet.contains(charArray[right])) {
                    hashSet.remove(charArray[left]);
                    left++;
                    size--;
                }
                hashSet.add(charArray[right]);
                right++;
                size++;

            }
        }
        return max_size;
    }
}
